package notice.service.impl;

import lombok.extern.slf4j.Slf4j;
import notice.dto.OrderNoticeDto;
import notice.service.Observer;
import notice.util.IdempotentUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @description: 扣款通知：风控处理（拉黑、M、N）
 * @author: zks
 * @since: 2022-08-17 10:19
 */
@Slf4j
@Service
public class ParwarningHandler implements Observer {
    //@Resource
    //private TruckCardBlackService cardBlackService;
    //@Resource
    //private IEarlyWarningRecordService earlyWarningRecordService;
    //@Resource
    //private TruckDeductionRecordServiceImpl truckDeductionRecordServiceImpl;
    @Resource
    private IdempotentUtil idempotentUtil;

    @Override
    public void update(OrderNoticeDto notice) {
        log.info("扣款通知-风控处理 notice={}", notice);
        // 幂等检查
        if (idempotentUtil.idempotentCheck(this.getClass(), notice.getOrderNo())) {
            log.warn("扣款通知-垫资处理 该记录已经处理过 notice={}", notice);
            return;
        }

        // 客车
//        if(OrderNoticeDto.ORDER_TYPE_CAR.equals(notice.getOrderType())) {
//            // 微信扣款风控处理
//            if(CarDeductionRecord.CHANNEL_WX.equals(notice.getCurrPayChannel())) {
//                carWXDeductionParwarning(notice);
//            }
//        } else {// 货车
//            // 微信扣款风控处理
//            if(TruckDeductionRecord.CHANNEL_WX.equals(notice.getCurrPayChannel())) {
//                truckWXDeductionParwarning(notice);
//            }
//            // 钱包扣款风控处理-下次兼容
//        }

        //  幂等表-有效期一周
        idempotentUtil.addIdempotentRecord(this.getClass(), notice.getOrderNo(), 7 * 24 * 60 * 60L);
    }

//    private void truckWXDeductionParwarning(OrderNoticeDto notice) {
//        log.info("扣款通知-风控处理-货车微信扣款 notice={}",notice);
//        //预付卡不拉黑名单 招商货车/新疆货车拉黑
//        if (EtcIssueTypeEnum.ZS_TRUCK.getCode().equals(notice.getEtcTypeId())
//                || EtcIssueTypeEnum.XJ_TRUCK.getCode().equals(notice.getEtcTypeId())) {
//            if (TruckDeductionRecord.PAY_STATUS_ACCEPT.equals(notice.getPayStatus())
//                    || TruckDeductionRecord.PAY_STATUS_FAIL.equals(notice.getPayStatus())) {
//                //如果代扣失败，先调用拉黑接口，在保存代扣结果，防止拉黑失败
//                cardBlackService.addBlack(notice.getPlateNumber(), notice.getEtcNo(), TruckCardBlackLog.USER_ID_SYSTEM, TruckCardBlackLog.USER_NAME_SYSTEM, "代扣失败", TruckCardBlack.SCENE_TRANS);
//                TruckDeductionRecord deductionRecord = new TruckDeductionRecord();
//                BeanUtils.copyProperties(notice,deductionRecord);
//                truckDeductionRecordServiceImpl.sendMsg(deductionRecord);
//            } else if (TruckDeductionRecord.PAY_STATUS_SUCCES.equals(notice.getPayStatus())
//                    && TruckDeductionRecord.PAY_STATUS_ACCEPT.equals(notice.getPayStatus())) {
//                //对已受理未支付的情况，查询后又支付了的，需要解黑
//                cardBlackService.removeBlackAndBusi(notice.getEtcNo(), TruckCardBlackLog.USER_ID_SYSTEM, TruckCardBlackLog.USER_NAME_SYSTEM, "代扣成功", TruckCardBlack.SCENE_TRANS);
//            }
//        }
//    }
//
//    private void carWXDeductionParwarning(OrderNoticeDto notice) {
//        log.info("扣款通知-风控处理-客车微信扣款 notice={}",notice);
//        if (CarDeductionRecord.PAY_STATUS_FAIL.equals(notice.getPayStatus())
//                && EtcIssueTypeEnum.ZS_CAR.getCode().equals(notice.getEtcTypeId())) {
//            //如果代扣失败，先调用拉黑接口，在保存代扣结果，防止拉黑失败
//            cardBlackService.addBlack(notice.getPlateNumber(), notice.getEtcNo(), TruckCardBlackLog.USER_ID_SYSTEM, TruckCardBlackLog.USER_NAME_SYSTEM, "代扣失败", TruckCardBlack.SCENE_TRANS);
//        }
//        if(EtcIssueTypeEnum.ZS_CAR_BEFOREHAND.getCode().equals(notice.getEtcTypeId())) {
//            //如果微信支付失败并且重试是默认状态 则添加M/N记录
//            if (CarDeductionRecord.PAY_STATUS_FAIL.equals(notice.getPayStatus())
//                    && CarDeductionRecord.DEFAULT_STATUS.equals(notice.getRetryStatus())) {
//                EarlyWarningRecordAddDto recordAddDt = new EarlyWarningRecordAddDto();
//                BeanUtils.copyProperties(notice,recordAddDt);
//                recordAddDt.setCarType(2);
//                List<Integer> warningTypes = new ArrayList<>();
//                warningTypes.add(WarningTypeEnum.WARNING_VALUE.getCode());
//                warningTypes.add(WarningTypeEnum.PULL_BLACK.getCode());
//                recordAddDt.setWarningTypes(warningTypes);
//                //增加M/N记录 不通知风控
//                try {
//                    log.info("招商预付卡-客车-微信首次扣费失败,开始增加风控M/N记录失败异常,orderNo={},recordAddDt={}", notice.getOrderNo(),recordAddDt);
//                    earlyWarningRecordService.insertWarningConfig(recordAddDt);
//                    log.info("招商预付卡-客车-微信首次扣费失败,结束增加风控M/N记录失败异常,orderNo={},recordAddDt={}", notice.getOrderNo(),recordAddDt);
//                } catch (Exception e) {
//                    log.error("招商预付卡-客车-微信首次扣费失败,增加风控M/N记录失败异常,orderNo={}", notice.getOrderNo(),e);
//                }
//            }
//        }
//    }
}
